const actionMixin = {
  data() {
    return {
      actionApi: {}
    }
  },
  methods: {
    /** 操作通用处理方法 */
    async handleAction(action, id, confirmMessage, successMessage) {
      const confirmOptions = {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        cancelButtonClass: 'btn-cancel-to-right',
        type: 'warning'
      };

      try {
        await this.$confirm(confirmMessage, this.$t('warning'), confirmOptions);
        const api = this.actionApi[action]
        const resp = await api(id);
        if (resp.success) {
          this.targaMessage(successMessage);
          this.getList();
        } else {
          this.targaMessageBox(this, resp.msg);
        }
      } catch (err) {
        console.error(err);
      }
    },

    async handleBatchAction(action, data) {
      const confirmOptions = {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        cancelButtonClass: 'btn-cancel-to-right',
        type: 'warning'
      };

      try {
        await this.$confirm(this.$t('executeConfirm'), this.$t('warning'), confirmOptions);
        const api = this.actionApi[action]
        const resp = await api(data);
        if (resp.success) {
          this.targaMessage(this.$t('executeSuccess'));
          this.getList();
        } else {
          this.targaMessageBox(this, resp.msg);
        }
      } catch (err) {
        console.error(err);
      }
    }
  }
}

export default actionMixin
